package com.wjs.gulimall.gulimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wjs.gulimall.gulimallproduct.VO.AttrRespVo;
import com.wjs.gulimall.gulimallproduct.VO.AttrVo;
import com.wjs.gulimall.gulimallproduct.entity.ProductAttrValueEntity;
import com.wjs.gulimall.gulimallproduct.service.ProductAttrValueService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wjs.gulimall.gulimallproduct.entity.AttrEntity;
import com.wjs.gulimall.gulimallproduct.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 商品属性
 *
 * @author wangjingsong
 * @email wangjingsong@gmail.com
 * @date 2022-04-17 15:11:00
 */
@RestController
@RequestMapping("gulimallproduct/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    //http://localhost:88/api/gulimallproduct/attr/base/list/0?t=1657273196395&page=1&limit=10&key=
    ///product/attr/base/listforspu/{spuId}
    //请求方法: GET
    ///product/attr/update/{spuId}

    /**
     * 查询spu的相关规格信息
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId")Long spuId){
        List<ProductAttrValueEntity> list=productAttrValueService.listforspu(spuId);
        return R.ok().put("data",list);
    }
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId")Long catelogId,
                      @PathVariable("attrType")String type)
    {
        PageUtils page =attrService.quertBaseList(params,catelogId,type);
        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo attr = attrService.getInfoById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    @RequestMapping("/update/{spuId}")
    public R updateSpu(@PathVariable("spuId") Long spuId,
                       @RequestBody List<ProductAttrValueEntity> items){
        productAttrValueService.updateSpu(spuId,items);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
